packages <- c(
  "sp.scRNAseq", "sp.scRNAseqData", "seqTools", "printr",
  "ggthemes", "tidyverse"
)
purrr::walk(packages, library, character.only = TRUE)
rm(packages)

##DATA
load('../data/uObjs.rda')
s <- str_detect(colnames(countsMgfp), "^s")
commonGenes <- intersect(rownames(countsMgfp), rownames(countsRegev))

sng <- cbind(countsMgfp[commonGenes, s], countsRegev[commonGenes, ])
mul <- countsMgfp[commonGenes, !s]

erccSng <- cbind(
  countsMgfpERCC[, s], 
  matrix(NA, nrow = nrow(countsMgfpERCC), ncol = ncol(countsRegev))
)
erccMul <- cbind(countsMgfpERCC[, !s])

boolMulC <- colnames(mul) %in% filter(countsMgfpMeta, tissue == "colon")$sample
boolSngC <- colnames(sng) %in% filter(countsMgfpMeta, tissue == "colon")$sample

#setup spCounts
cObjSng <- spCounts(sng, erccSng)
## is.na(c(counts, counts.ercc) returned TRUE
cObjMul <- spCounts(mul, erccMul)

cObjSngSi <- spCounts(sng[, !boolSngC], erccSng[, !boolSngC])
## is.na(c(counts, counts.ercc) returned TRUE
cObjSngC <- spCounts(sng[, boolSngC], erccSng[, boolSngC])

cObjMulSi <- spCounts(mul[, !boolMulC], erccMul[, !boolMulC])
cObjMulC <- spCounts(mul[, boolMulC], erccMul[, boolMulC])

Colon

plotUnsupervisedClass(uObjC, cObjSngC)

plotUnsupervisedMarkers(uObjC, cObjSngC, c("Lgr5", "Muc2", "Ptprc", "Chga", "Slc40a1"))

plotUnsupervisedMarkers(uObjC, cObjSngC, c("Mki67"))

Small Intestine

plotUnsupervisedClass(uObjSi, cObjSngSi)

plotUnsupervisedMarkers(
  uObjSi, cObjSngSi, 
  c("Lgr5", "Muc2", "Ptprc", "Chga", "Alpi", "Lyz1", "Dclk1"), 
  pal = RColorBrewer::brewer.pal(7, "Set2")
)

plotUnsupervisedMarkers(uObjSi, cObjSngSi, c("Mki67"))

All

plotUnsupervisedClass(uObj, cObjSng)

plotUnsupervisedMarkers(
  uObj, cObjSng,
  c("Lgr5", "Muc2", "Ptprc", "Chga", "Alpi", "Lyz1", "Dclk1", "Slc40a1"), 
  pal = RColorBrewer::brewer.pal(8, "Set1")
)

plotUnsupervisedClass(uObj, cObjSng) %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(tissue = if_else(str_detect(Sample, "SRR"), "SI", tissue)) %>%
  ggplot() +
  geom_point(aes(`t-SNE dim 1`, `t-SNE dim 2`, colour = tissue)) +
  ggthemes::theme_few()

plotUnsupervisedClass(uObj, cObjSng) %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(tissue = if_else(str_detect(Sample, "SRR"), "SI", tissue)) %>%
  mutate(plate = if_else(is.na(plate), "Regev", plate)) %>%
  ggplot() +
  geom_point(aes(`t-SNE dim 1`, `t-SNE dim 2`, colour = plate), alpha = 0.75) +
  ggthemes::theme_few() +
  scale_colour_manual(values = col40())

plotUnsupervisedClass(uObj, cObjSng) %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(total.counts = map_dbl(Sample, function(s) {
    sum(getData(cObjSng, "counts")[, s])
  })) %>%
  mutate(total.counts.bin = ntile(total.counts, 10)) %>%
  group_by(total.counts.bin) %>%
  mutate(total.counts.mean = mean(total.counts)) %>%
  ggplot() +
  geom_point(aes(`t-SNE dim 1`, `t-SNE dim 2`, colour = total.counts.mean)) +
  ggthemes::theme_few() +
  viridis::scale_colour_viridis(option = "E") +
  guides(colour = guide_colorbar(title = "Binned total counts")) +
  labs(caption = paste0(
    "Total counts were calculated for each sample. The total counts were then ",
    "evenly divided into 10 bins for the whole dataset and the mean\n ",
    "total counts calculated for each bin. This mean was then portrayed as the ",
    "point colour in the figure."
  )) +
  theme(
    plot.caption = element_text(hjust = 0)
  )

plotUnsupervisedClass(uObj, cObjSng) %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(total.counts = map_dbl(Sample, function(s) {
    sum(getData(cObjSng, "counts")[, s])
  })) %>%
  mutate(total.counts.bin = ntile(total.counts, 10)) %>%
  group_by(total.counts.bin) %>%
  mutate(total.counts.mean = mean(total.counts)) %>%
  mutate(Regev = if_else(str_detect(Sample, "SRR"), TRUE, FALSE)) %>%
  ggplot() + 
  geom_boxplot(aes(Regev, total.counts)) +
  theme_bw()

Paneth

plotUnsupervisedMarkers(uObj, cObjSng, "Lyz1") %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(regev = if_else(str_detect(Sample, "SRR"), TRUE, FALSE)) %>%
  filter(Classification == "S1") %>%
  ggplot() +
  geom_point(
    aes(`t-SNE dim 1`, `t-SNE dim 2`, shape = regev, colour = Lyz1), 
    size = 3
  ) +
  viridis::scale_colour_viridis(option = "E") + 
  theme_few()

plotUnsupervisedMarkers(uObj, cObjSng, "Defa24") %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(regev = if_else(str_detect(Sample, "SRR"), TRUE, FALSE)) %>%
  filter(Classification == "S1") %>%
  ggplot() +
  geom_point(
    aes(`t-SNE dim 1`, `t-SNE dim 2`, shape = regev, colour = Defa24), 
    size = 3
  ) +
  viridis::scale_colour_viridis(option = "E") + 
  theme_few()

plotUnsupervisedMarkers(uObj, cObjSng, "Defa17") %>%
  plotData() %>%
  full_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  filter(!is.na(`t-SNE dim 1`)) %>%
  mutate(regev = if_else(str_detect(Sample, "SRR"), TRUE, FALSE)) %>%
  filter(Classification == "S1") %>%
  ggplot() +
  geom_point(
    aes(`t-SNE dim 1`, `t-SNE dim 2`, shape = regev, colour = Defa17), 
    size = 3
  ) +
  viridis::scale_colour_viridis(option = "E") + 
  theme_few()

d <- plotUnsupervisedClass(uObj, cObjSng) %>%
  plotData() %>%
  inner_join(countsMgfpMeta, by = c("Sample" = "sample")) %>%
  select(Sample, Classification, plate:column, mouseID) %>%
  filter(Classification == "S1") %>%
  mutate(total.counts = map_dbl(Sample, function(s) {
    sum(getData(cObjSng, "counts")[, s])
  })) %>%
  mutate(Lyz1.cpm = map_dbl(Sample, function(s) {
    getData(cObjSng, "counts")["Lyz1", s]
  })) %>%
  mutate(Defa24.cpm = map_dbl(Sample, function(s) {
    getData(cObjSng, "counts")["Defa24", s]
  })) %>%
  mutate(Defa17.cpm = map_dbl(Sample, function(s) {
    getData(cObjSng, "counts")["Defa17", s]
  }))

as.data.frame(d)
Sample Classification plate row column mouseID total.counts Lyz1.cpm Defa24.cpm Defa17.cpm
s.NJA00801.B06 S1 NJA00801 2 6 8 53349 1301 499 73
s.NJA00801.G06 S1 NJA00801 7 6 8 213320 1294 644 90
s.NJA01202.E01 S1 NJA01202 5 1 12 71409 1612 634 90
s.NJA01202.B03 S1 NJA01202 2 3 12 30331 852 549 78
s.NJA01202.K04 S1 NJA01202 11 4 12 48679 1489 633 83
s.NJA01202.C01 S1 NJA01202 3 1 12 41817 1737 631 82